package co.yixiang.yshop.module.product.controller.admin.producttag;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.producttag.param.*;
import co.yixiang.yshop.module.product.controller.admin.producttag.vo.ProductTagViewRespVO;
import co.yixiang.yshop.module.product.dal.dataobject.producttag.ProductTagGroupDO;
import co.yixiang.yshop.module.product.service.producttag.ProductTagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商品标签&标签组")
@RestController
@RequestMapping("/product/tag")
@Validated
public class ProductTagController {

    @Resource
    private ProductTagService productTagService;


    @PostMapping("/create")
    @Operation(summary = "创建标签")
    @PreAuthorize("@ss.hasPermission('product:tag:create')")
    public CommonResult<Integer> create(@Validated @RequestBody ProductTagCreateReqParam vo) {
        return success(productTagService.createTag(vo));
    }

    @GetMapping("/page")
    @Operation(summary = "标签列表分页")
    @PreAuthorize("@ss.hasPermission('product:tag:list')")
    public CommonResult<PageResult<ProductTagViewRespVO>> page(@Validated ProductTagSearchReqParam vo) {
        return success(productTagService.tagPage(vo));
    }

    @PutMapping("/update")
    @Operation(summary = "修改标签")
    @PreAuthorize("@ss.hasPermission('product:tag:update')")
    public CommonResult<Boolean> update(@Validated @RequestBody ProductTagUpdateReqParam vo) {
        return success(productTagService.updateTag(vo));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除标签")
    @PreAuthorize("@ss.hasPermission('product:tag:delete')")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        return success(productTagService.deleteTag(id));
    }

    @PostMapping("/group/create")
    @Operation(summary = "创建标签组")
    @PreAuthorize("@ss.hasPermission('product:tagGroup:create')")
    public CommonResult<Integer> createGroup(@Validated @RequestBody ProductTagGroupCreateParam vo) {
        return success(productTagService.createTagGroup(vo));
    }

    @PutMapping("/group/update")
    @Operation(summary = "修改标签组")
    @PreAuthorize("@ss.hasPermission('product:tagGroup:update')")
    public CommonResult<Integer> updateGroup(@Validated @RequestBody ProductTagGroupUpdateParam vo) {
        return success(productTagService.updateTagGroup(vo));
    }

    @GetMapping("/group/list")
    @Operation(summary = "查询标签组")
    @PreAuthorize("@ss.hasPermission('product:tagGroup:list')")
    public CommonResult<List<ProductTagGroupDO>> listGroup() {
        return success(productTagService.listTagGroup());
    }

    @DeleteMapping("/group/delete/{id}")
    @Operation(summary = "删除标签组")
    @PreAuthorize("@ss.hasPermission('product:tagGroup:delete')")
    public CommonResult<Integer> deleteGroup(@PathVariable Long id) {
        return success(productTagService.deleteTagGroup(id));
    }

}
